package com.googlecode.archimedes.filegen.model;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Models an application.
 * 
 * @author John Lema
 * @author Erik Gustavson
 * @version $Id: Application.java 2088 2006-03-16 01:56:24 +0000 (Thu, 16 Mar 2006) erik $
 */
public class Application extends AbstractAsset {

	private List<Entity>		entities		= new ArrayList<Entity>();
	private List<Tab>			tabs			= new ArrayList<Tab>();
	private List<Interface>		interfaces		= new ArrayList<Interface>();
	private List<Enumeration>	enumerations	= new ArrayList<Enumeration>();
	private String				name;
	private String				displayName;
	private String				logoUrl			= "/img/archimedes.png";
	private String				revision		= "$Revision: 2088 $";
	private String				basePackage		= "com";

	/**
	 * Returns the revision.
	 * 
	 * @return String value of revision
	 */
	public String getRevision() {
		return revision;
	}

	public String getParsedRevision() {

		MatchResult result = null;

		// create our pattern
		Pattern pattern = Pattern.compile(":\\s(.*)\\s\\$$");

		Matcher matcher = pattern.matcher(getRevision());

		if (matcher.find()) {
			result = matcher.toMatchResult();
			log.debug(String.format("Found:%n------%n%s %n%nat (%d,%d)%n", result.group(1), result.start(), result.end()));
		}

		return result != null ? result.group(1) : "1.0";
	}

	/**
	 * Sets the revision
	 * 
	 * @param revision
	 *            The revision to set.
	 */
	public void setRevision(String revision) {
		this.revision = revision;
	}

	/**
	 * Returns the logoUrl.
	 * 
	 * @return String value of logoUrl
	 */
	public String getLogoUrl() {
		return logoUrl;
	}

	/**
	 * Sets the logoUrl
	 * 
	 * @param logoUrl
	 *            The logoUrl to set.
	 */
	public void setLogoUrl(String logoUrl) {
		this.logoUrl = logoUrl;
	}

	/**
	 * @param boName
	 * @return the BizObject of the same name reutns null if not found
	 */
	public Entity getEntityByName(String entityName) {

		for (Entity entity : getEntities()) {
			if (entity.getEntityName().equals(entityName)) {
				return entity;
			}
		}

		return null;
	}

	/**
	 * Adds a new entity to the application.
	 * 
	 * @param entity
	 */
	public void addEntity(Entity entity) {
		getEntities().add(entity);
	}

	/**
	 * @return
	 */
	public List<Entity> getEntities() {
		return this.entities;
	}

	/**
	 * @param bos
	 */
	public void setEntities(List<Entity> entities) {
		this.entities = entities;
	}

	/**
	 * @return
	 */
	public String getDisplayName() {
		return displayName;
	}

	/**
	 * @param displayName
	 */
	public void setDisplayName(String displayName) {
		this.displayName = displayName;
	}

	/**
	 * @return
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 */
	public void setName(String name) {
		this.name = name;
	}

	// /**
	// * @return
	// */
	// public List getRelationships() {
	// return relationships;
	// }
	//
	// /**
	// * @param relationships
	// */
	// public void setRelationships(List<Relationship> relationships) {
	// this.relationships = relationships;
	// }
	//
	// public void addRelationship(Relationship rel) {
	// this.relationships.add(rel);
	// }

	/**
	 * Returns the tabs.
	 * 
	 * @return List<Tab> value of tabs
	 */
	public List<Tab> getTabs() {
		return tabs;
	}

	/**
	 * Sets the tabs
	 * 
	 * @param tabs
	 *            The tabs to set.
	 */
	public void setTabs(List<Tab> tabs) {
		this.tabs = tabs;
	}

	/**
	 * Returns a list of all subodinateAssets. For an Application, this list contains Entities.
	 * 
	 * @see com.tps.filegen.model.Asset#getSubordinateAssets()
	 */
	public Collection<? extends Asset> getSubordinateAssets() {
		List<Asset> results = new ArrayList<Asset>();
		results.addAll(getEntities());
		results.addAll(getInterfaces());
		results.addAll(getEnumerations());
		return results;
	}

	/**
	 * Returns the interfaces.
	 * 
	 * @return List<Interface> value of interfaces
	 */
	public List<Interface> getInterfaces() {
		return interfaces;
	}

	/**
	 * Sets the interfaces
	 * 
	 * @param interfaces
	 *            The interfaces to set.
	 */
	public void setInterfaces(List<Interface> interfaces) {
		this.interfaces = interfaces;
	}

	/**
	 * Returns the enumerations.
	 * 
	 * @return List<Enumeration> value of enumerations
	 */
	public List<Enumeration> getEnumerations() {
		return enumerations;
	}

	/**
	 * Sets the enumerations
	 * 
	 * @param enumerations
	 *            The enumerations to set.
	 */
	public void setEnumerations(List<Enumeration> enumerations) {
		this.enumerations = enumerations;
	}

	/**
	 * @param basePackage
	 *            the basePackage to set
	 */
	public void setBasePackage(String basePackage) {
		this.basePackage = basePackage;
	}

	/**
	 * @return the basePackage
	 */
	public String getBasePackage() {
		return basePackage;
	}

}